#ifndef TEXTEDITOR_H
#define TEXTEDITOR_H

#include <QPlainTextEdit>
#include <QFileDialog>
#include <QPainter>
#include <htmlsyntaxhighlighter.h>

class LineNumberArea;

class TextEditor : public QPlainTextEdit
{
    Q_OBJECT
public:
    explicit TextEditor(QString filename, QWidget *parent = 0);

    QString filename() const { return filename_; }

    void paintLineNumberArea(QPaintEvent * event);

protected:
    void resizeEvent(QResizeEvent * event);

private:
    QString filename_;
    LineNumberArea * lineNumberArea_;
    int lineNumberAreaWidth_;
    int selectionStart_, selectionEnd_;
    HtmlSyntaxHighlighter * highlighter_;
    
signals:
    void saved();

public slots:
    bool saveFile();
    bool saveAs();
    void openFile();
    
private slots:
    void updateLineNumberArea(QRect rect, int dy);
    void updateLineNumberAreaWidth(int blockcount);
    void updateCursorPosition();
    void updateCursorSelection();
};

class LineNumberArea : public QWidget
{
    Q_OBJECT
public:
    LineNumberArea(TextEditor * parent);

    int spacing() const { return spacing_; }

protected:
    void paintEvent(QPaintEvent * event);

private:
    TextEditor * editor_;
    static const int spacing_ = 5;
};

#endif // TEXTEDITOR_H
